// pages/index/login.js
var app=getApp()
Page({

  data: {      phone:'',yanzhengma2:'',getCode:'获取验证码',disabled:'false',uid:'',flag:false,pid:'',id:'',last:'',flag1:'',select:false,
  filemodal:false

  },

  onLoad: function (options) {
    this.getyinsi()
    this.setData({
      avatarUrl:options.avatarUrl,
      nickName:options.nickName,
      openid:options.openid,
      type:options.type,
     
      
    })
    if(options.id){
      this.setData({id:options.id})
    }
    if(options.pid){
      this.setData({pid:options.pid})
    }
    
  },
  change(e){
    this.checkphone(e.detail.value)
    this.setData({
      phone:e.detail.value
      
    })
  },
  change1(e){
    this.setData({
      yanzhengma2:e.detail.value
    })
  },
  change3(e){
  let testphone=this.checkphone1(e.detail.value)
  console.log(testphone);
  this.setData({phone:e.detail.value})
  },
  time(e){
     if(this.data.flag==false){return false}

    var disabled = e.currentTarget.dataset.disabled 
    if(disabled == true){
      return false;
    }
    this.throttle(()=>{
      this.getrecoginize()
    },1500)
   
  },
  toggleselect(){
    let select=this.data.select
    this.setData({
      select:!select
    })
  },
  login(){
    
    if(this.data.flag==false||this.data.yanzhengma2==''){return false}
    if(!this.data.select){
       wx.showToast({
         title: '请勾选协议',
         icon:'none'
       })
       return
    }
    let yanzhengma2=wx.getStorageSync('yanzhengma2')
    const params={img:this.data.avatarUrl,username:this.data.nickName,openid:this.data.openid,type:this.data.type,phone:this.data.phone,pid:this.data.pid}
    if(this.data.yanzhengma2==yanzhengma2){
      wx.request({
        url: 'https://zujieba.com/Api/wxlogin/login.html',
        data:params,
        success:(res1)=>{
          if(res1.data.success==1){  console.log(res1);
            app.globalData.coupon=res1.data.data.coupon
            
            this.setData({uid:res1.data.data.uid})
            wx.setStorageSync('uid', res1.data.data.uid)
            wx.setStorageSync('avatarUrl', this.data.avatarUrl)
            wx.setStorageSync('nickName',this.data.nickName)
            wx.setStorageSync('phone',res1.data.data.phone)
            wx.setStorageSync('loginflag',true)

            
            wx.showToast({
              title: res1.data.tips,
              icon:"success",
              duration:2000
            })
           
           
            　
                
                  if(this.data.id!=""){
                  wx.request({
                    url:'https://zujieba.com/Api/bind/bind.html',
                    data:{pid:this.data.id,uid:res1.data.data.uid},
                    method:'POST',
                    success:(res)=>{
                      if(res.data.success==1){
                         wx.showToast({title:'绑定成功'})
                      }
                    }
                  })}
                  wx.navigateBack({
                    delta:2,
                    complete: (res) => {},
                  })
  
           }else{
            console.log('success!=1的时候');
             wx.showToast({
               
               
               title: res1.data.tips,
               icon:'none'
             })
           }
        
          
         


        },
        fail:(res)=>{
          console.log(res,"登陆失败fail路线");
          
          wx.showToast({
            title: res.data.tips,
            icon:"none"
          })
        }
      })
      
    }else{
      wx.showToast({
        title: '验证码出错',
        icon:'none'
      })
    }
    
  },
   //获取验证码
   getrecoginize(){
     
    wx.request({
      url: 'https://zujieba.com/Api/phone/code.html',
      data:{phone:this.data.phone},
      method:'POST',
      success:(res)=>{
        console.log(res);
        if(res.data.success==2){
            wx.showToast({
              title: res.data.tips,
              icon:'none'
            })
        }
       if(res.data.success==1){
        wx.setStorageSync('yanzhengma2', res.data.data)
       }
        
      },
      fail:(res)=>{
        console.log(res);
        wx.showToast({
          title: '发送失败',
          icon:'none'
        })
        
      }
    })
    var that = this;
    var times = 60
    var i = setInterval(function() {
         times--
         if (times <= 0) {
           that.setData({
             color: "#ff6f10",
             disabled: false,
             getCode: "获取验证码",
        })
        clearInterval(i)
            
         } else {
             
           
          that.setData({
           getCode: "重新获取" + times + "s",
           color: "#999",
           disabled: true
      })
         }
    }, 1000)

    
     
},
checkphone(phone){
  if(!(/^1[345789]\d{9}$/.test(phone))){ 
    wx.showToast({
      title: '请输入正确手机号',
     
      icon:'none'
    })
     this.setData({flag:false})
    return false; 

} else{
  this.setData({flag:true})
  return true
}
},
checkphone1(phone){
  if(!(/^1[345789]\d{9}$/.test(phone))){ 
   
     this.setData({flag:false})
    return false; 

} else{
  this.setData({flag:true})
  return true
}
},
onShareAppMessage(){
  console.log(11);
  
},
throttle:function(func,wait){
  console.log(func,wait);
  
  let last =this.data.last
  console.log(last);
  
  let current_time= +new Date()
  if(current_time-last>wait){
    console.log(1111);
      console.log(current_time,current_time-last);
      
      func()
      last=+new Date()
      this.setData({
        last
      })
  }
    
     
  
   
},
showfilemodal(){
console.log(1);

  wx.navigateTo({
    url: '/pages/mine/yinsi',
  })
},
getyinsi(){
    wx.request({
      url: 'https://zujieba.com/Api/info/info.html',
      data:{id:1},
      method:"POST",
      success:(res)=>{
        console.log(res);
        this.setData({
          nodes:res.data.data.info
        })
        
      }
    })
},
closefilemodal(){
   this.setData({
       filemodal:false
   })
}
 
})